package self.hoy.demo.grainmall.ware.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import self.hoy.demo.common.to.es.SkuHasStockVo;
import self.hoy.demo.common.utils.PageUtils;
import self.hoy.demo.common.utils.Query;

import self.hoy.demo.common.utils.R;
import self.hoy.demo.grainmall.ware.feign.ProductFeignService;
import self.hoy.demo.grainmall.ware.mapper.WareSkuMapper;
import self.hoy.demo.grainmall.ware.entity.WareSku;
import self.hoy.demo.grainmall.ware.service.WareSkuService;
import self.hoy.demo.grainmall.ware.vo.StockVo;

import javax.annotation.Resource;


@Service("wareSkuService")
public class WareSkuServiceImpl extends ServiceImpl<WareSkuMapper, WareSku> implements WareSkuService {

    @Resource
    private WareSkuMapper wareSkuMapper;

    @Autowired
    private ProductFeignService productFeignService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper<WareSku> wrapper = new QueryWrapper<>();
        String skuId = (String) params.get("skuId");
        if (!StringUtils.isEmpty(skuId)) {
            wrapper.eq("sku_id", skuId);
        }
        String wareId = (String) params.get("wareId");
        if (!StringUtils.isEmpty(wareId)) {
            wrapper.eq("ware_id", wareId);
        }

        IPage<WareSku> page = this.page(
                new Query<WareSku>().getPage(params), wrapper
        );

        return new PageUtils(page);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)//如果当前存在事务，则加入该事务，如果当前不存在事务，则创建一个新的事务。
    public void addStock(Long skuId, Long wareId, Integer skuNum) {
        List<WareSku> list = wareSkuMapper.selectList(new QueryWrapper<WareSku>().eq("sku_id", skuId).eq("ware_id", wareId));
        if (list == null || list.isEmpty()) {
            WareSku sku = new WareSku();
            try {
                R info = productFeignService.info(skuId);
                if (info.getCode() == 0) {
                    Map<String, Object> data = (Map<String, Object>) info.get("skuInfo");
                    sku.setSkuName((String) data.get("skuName"));
                }
            } catch (Exception e) {
                //TODO 异常不回滚
            }
            sku.setSkuId(skuId);
            sku.setStock(skuNum);
            sku.setWareId(wareId);
            sku.setStockLocked(0);
            wareSkuMapper.insert(sku);
        } else {
            wareSkuMapper.addStock(skuId, wareId, skuNum);
        }
    }

    @Override
    public List<SkuHasStockVo> getSkuHasStock(List<Long> skuIds) {
        List<StockVo> list = wareSkuMapper.countStock(skuIds);
        return list.stream().map(s -> {
            SkuHasStockVo v = new SkuHasStockVo();
            v.setSkuId(s.getSkuId());
            v.setHasStock(s.getStock() > 0);
            return v;
        }).collect(Collectors.toList());
    }

}